﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;

namespace PRE.Catalog
{
    public partial class FrmTKSoLuongSachTheoDauSach : DevExpress.XtraEditors.XtraForm
    {
        #region Variable

        DAL.EntitiesDAL.DauSachDAL _dal = new DAL.EntitiesDAL.DauSachDAL();
        BLL.EntitiesBLL.DauSachBLL _bll = new BLL.EntitiesBLL.DauSachBLL();

        DataTable _dt = new DataTable();
        DataTable _dt1 = new DataTable();
      
        #endregion

        #region Inits

        public FrmTKSoLuongSachTheoDauSach()
        {
            InitializeComponent();
        }

        #endregion

        #region Form Load

        private void FrmTKSoLuongSachTheoDauSach_Load_1(object sender, EventArgs e)
        {
            PerformRefresh();
            btnGoSearch.Visible = true;
            btn_In.Visible = true;
            btn_Thoat.Visible = true;
        }

        #endregion

        #region load dữ liệu
        private void PerformRefresh()
        {
            LoadData();
        }

        private void LoadData()
        {
            _dt = _dal.Select1();
            _dt1 = _dal.Select();

            cbo_MaDauSach.Properties.DataSource = _dt1;

            try
            {
                if (_dt != null)
                {
                    gridControl_TheoDauSach.DataSource = _dt;

                    AppGridView.SummaryField(gridView_TheoDauSach, "MACUONSACH", "Tổng = {0:#,0} mẫu tin", DevExpress.Data.SummaryItemType.Count);
                    AppGridView.ReadOnlyColumn(gridView_TheoDauSach,
                        new string[] {"MACUONSACH","TENDAUSACH", "NOIDUNG_TT", "LANXUATBAN", "KICHTHUOC", "SOTRANG", "TENKESACH"
                           , "TENTHELOAI", "TENNHAXB"});

                    gridView_TheoDauSach.OptionsBehavior.ReadOnly = true;
                    gridView_TheoDauSach.OptionsView.ColumnAutoWidth = true;
                }

            }
            catch (Exception ex)
            {

                XtraMessageBox.Show("Lỗi! Không load được dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        #endregion

        #region Button Thoát
        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion 

        #region lọc dữ liệu
        private void btnGoSearch_Click(object sender, EventArgs e)
        {
            try
            {
                string Chondausach = Convert.ToString(cbo_MaDauSach.EditValue);
                DataTable _dtSearch = _dal.Search_TheoMaDauSach(Chondausach);
                if (_dtSearch != null)
                {
                   

                    gridControl_TheoDauSach.DataSource = _dtSearch;

                    AppGridView.SummaryField(gridView_TheoDauSach, "MACUONSACH", "Tổng = {0:#,0} mẫu tin", DevExpress.Data.SummaryItemType.Count);
                    AppGridView.ReadOnlyColumn(gridView_TheoDauSach,
                        new string[] { "MACUONSACH","TENDAUSACH", "NOIDUNG_TT", "LANXUATBAN", "KICHTHUOC", "SOTRANG", "TENKESACH"
                           , "TENTHELOAI", "TENNHAXB" });

                    gridView_TheoDauSach.OptionsView.EnableAppearanceEvenRow = true;
                    gridView_TheoDauSach.OptionsView.ColumnAutoWidth = true;
                }
                gridControl_TheoDauSach.DataSource = _dtSearch;

                _dt = _dtSearch;

              
            }
            catch (Exception ex)
            {

                XtraMessageBox.Show("Lỗi! Không lọc được dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion 

        #region lam tuoi
        private void cbo_refresh_Click(object sender, EventArgs e)
        {
            FrmTKSoLuongSachTheoDauSach_Load_1(null, null);
        }
        #endregion 

        #region In dữ liệu
        private void btn_In_Click(object sender, EventArgs e)
        {
           
            string _NgayKy = "TP.Cần Thơ, ngày " + DateTime.Now.Day + " tháng " + DateTime.Now.Month + " năm " + DateTime.Now.Year;
            if (gridView_TheoDauSach.DataRowCount > 0)
            {
                DataTable dt = gridView_TheoDauSach.GetDataRow(0).Table.Clone();

                for (int i = 0; i < gridView_TheoDauSach.DataRowCount; i++)
                {
                    dt.ImportRow(gridView_TheoDauSach.GetDataRow(i));
                }
                Reports.FrmReportView frm = new Reports.FrmReportView();
                frm._load_XtraTKSLSachTheoDauSach(dt, _NgayKy);
                frm.Show();
            }
        #endregion

        }
    }
}
